package com.mall.httpResult;/*
 *统一所有controller返回结果为HttpResult
 *@author ojj
 *@date 2020/4/18 16:10
 *
 */

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

@Getter
@Setter
@ToString
public class HttpResult <T> implements Serializable {
    private boolean success;
    private Integer code;
    private T data;
    private String message;
    private HttpResult(){
        this.code = 200;
        this.success = true;
    }
    private HttpResult(T t){
        this.code = 200;
        this.success = true;
        this.data = t;
    }
    private HttpResult(ResultCodeEnum resultCodeEnum){
        this.code = resultCodeEnum.getCode();
        this.success = false;
        this.message = resultCodeEnum.getMessage();
    }
    public static<T> HttpResult<T> success(T data){
        return new HttpResult<T>(data);
    }

    public static<T> HttpResult<T> success(){
        return new HttpResult<T>();
    }

    public static<T> HttpResult<T> failure(ResultCodeEnum resultCodeEnum){
        return new HttpResult<T>(resultCodeEnum);
    }
    public static<T> HttpResult<T> failure(ResultCodeEnum resultCodeEnum , T data){
        HttpResult<T> httpResult = new HttpResult<T>(resultCodeEnum);
        httpResult.setData(data);
        return  httpResult;
    }
}
